package com.servlettest.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SurveyServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		request.setCharacterEncoding("utf-8");
		out.println("姓名：" + fiterHtml(request.getParameter("username"))
				+ "<br>");
		out.println("Email：" + fiterHtml(request.getParameter("Email"))
				+ "<br>");
		out.println("年龄：" + request.getParameter("age") + "<br>");
		out.println("编程时间：" + request.getParameter("code") + "<br>");
		out.println("操作系统：");
		String[] os = request.getParameterValues("os");
		out.println("<ul>");
		for (int i = 0; i < os.length; i++) {
			out.println("<li>" + os[i] + "</li>");
		}
		out.println("</ul><br>");
		out.println("编程语言：");
		String[] language = request.getParameterValues("language");
		out.println("<ul>");
		if (language != null) {
			for (int i = 0; i < language.length; i++) {
				out.println("<li>" + language[i] + "</li>");
			}
		} else {
			out.println("暂无");
		}
		out.println("</ul><br>");
		out.println("建议：" + fiterHtml(request.getParameter("comment")) + "<br>");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	public String fiterHtml(String value) {
		value = value.replaceAll("&", "&amp;");
		value = value.replaceAll("<", "&lt;");
		value = value.replaceAll(">", "&gt;");
		value = value.replaceAll(" ", "&nbsp;");
		value = value.replaceAll("'", "&#39");
		value = value.replaceAll("\"", "&quot;");
		value = value.replaceAll("\n", "<br>");

		return value;
	}

}
